/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Nov 22, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.gmatrix.toolkit;

import edu.psu.geovista.app.gmatrix.event.MatrixIndicationEvent;
import edu.psu.geovista.app.gmatrix.event.MatrixIndicationListener;
import edu.psu.geovista.app.gmatrix.event.MatrixSelectionEvent;
import edu.psu.geovista.app.gmatrix.event.MatrixSelectionListener;
import edu.psu.geovista.ui.event.IndicationEvent;
import edu.psu.geovista.ui.event.IndicationListener;
import edu.psu.geovista.ui.event.SelectionEvent;
import edu.psu.geovista.ui.event.SelectionListener;

import javax.swing.event.EventListenerList;
import java.awt.*;
import java.util.EventListener;
import java.util.Set;

public class FireEventSupport {
    protected Object src;//event source component

    public FireEventSupport(Object src) {
        this.src = src;
    }

    protected EventListenerList listenerList = new EventListenerList();
       /**
     * adds an SelectionListener
     */
    public void addListener (Class listenerClass, EventListener l) {
        listenerList.add(listenerClass, l);
    }

    /**
     * removes an SelectionListener from the component
     */
    public void removeListener (Class listenerClass,EventListener l) {
        listenerList.remove(listenerClass, l);
    }
    /*******************************************************************************************************
     *                common
     *******************************************************************************************************/
     public  void fireSelectionChanged (int[] newSelection) {

        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        SelectionEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == SelectionListener.class) {
                // Lazily create the event:
                if (e == null) {
                    e =new SelectionEvent(getSrc(), newSelection); //new PcpSelectionEvent(this, newSelection);
                    //e.setMode(mode);
                }
                ((SelectionListener)listeners[i + 1]).selectionChanged(e);
            }
        }//next i
     }
     public  void fireIndicationChanged (int indicator) {

        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        IndicationEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == IndicationListener.class) {
                // Lazily create the event:
                if (e == null) {
                    e = new IndicationEvent(getSrc(), indicator);
                }
                ((IndicationListener)listeners[i + 1]).indicationChanged(e);
            }
        }//next i

    }

     /*******************************************************************************************************
      *                for matrix canvas
      *******************************************************************************************************/
     public  void fireSelectionChanged (Set<Point> newSelection) {

        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        MatrixSelectionEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == MatrixSelectionListener.class) {
                // Lazily create the event:
                if (e == null) {
                    e =new MatrixSelectionEvent(getSrc(), newSelection); //new PcpSelectionEvent(this, newSelection);
                    //e.setMode(mode);
                }
                ((MatrixSelectionListener)listeners[i + 1]).matrixSelectionChanged(e);
            }
        }//next i
     }

     /* public  void fireSelectRowChanged(int[] rows, int selectType) {

        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        MatrixSelectionEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == MatrixSelectionListener.class) {
                // Lazily create the event:
                if (e == null) {
                    e =new MatrixSelectionEvent(getSrc(), MatrixSelectionEvent.SELECT_ROW); //new PcpSelectionEvent(this, newSelection);
                    e.setSelectrows(rows);
                    //e.setMode(mode);
                }
                ((MatrixSelectionListener)listeners[i + 1]).matrixSelectionChanged(e);
            }
        }//next i
     }*/

    /*  public  void fireSelectColumnChanged (int[] cols) {

        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        MatrixSelectionEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == MatrixSelectionListener.class) {
                // Lazily create the event:
                if (e == null) {
                    e =new MatrixSelectionEvent(getSrc(), MatrixSelectionEvent.SELECT_COL); //new PcpSelectionEvent(this, newSelection);
                    e.setSelectcols(cols);
                    //e.setMode(mode);
                }
                ((MatrixSelectionListener)listeners[i + 1]).matrixSelectionChanged(e);
            }
        }//next i
     }*/


     public  void fireIndicationChanged (Point indicator) {

        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        MatrixIndicationEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == MatrixIndicationListener.class) {
                // Lazily create the event:
                if (e == null) {
                    e = new MatrixIndicationEvent(getSrc(), indicator);
                }
                ((MatrixIndicationListener)listeners[i + 1]).matrixIndicationChanged(e);
            }
        }//next i

    }

    public Object getSrc() {
        return src;
    }
}
